package com.itheima.prize.api.action;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.prize.commons.db.entity.CardGame;
import com.itheima.prize.commons.db.entity.CardProductDto;
import com.itheima.prize.commons.db.entity.ViewCardUserHit;
import com.itheima.prize.commons.db.service.*;
import com.itheima.prize.commons.utils.ApiResult;
import com.itheima.prize.commons.utils.PageBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/api/game")
@Api(tags = {"活动模块"})
public class GameController {

    @Autowired
    private CardGameService gameService;
    @Autowired
    private CardProductService productService;
    @Autowired
    private CardGameProductService gameProductService;
    @Autowired
    private ViewCardUserHitService viewCardUserHitService;
    @Autowired
    private GameLoadService loadService;

    @GetMapping("/list/{status}/{curpage}/{limit}")
    @ApiOperation(value = "活动列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="status",value = "活动状态（-1=全部，0=未开始，1=进行中，2=已结束）",example = "-1",required = true),
            @ApiImplicitParam(name = "curpage",value = "第几页",defaultValue = "1",dataType = "int", example = "1",required = true),
            @ApiImplicitParam(name = "limit",value = "每页条数",defaultValue = "10",dataType = "int",example = "3",required = true)
    })
    public ApiResult list(@PathVariable int status,@PathVariable int curpage,@PathVariable int limit) {
        //TODO：任务4.1-活动模块-活的列表
        QueryWrapper<CardGame> queryWrapper = new QueryWrapper<>();
        if(status != -1){
            queryWrapper.eq("status", status);
        }
        queryWrapper.orderByDesc("starttime");
        Page<CardGame> page = new Page<>(curpage, limit);
        Page<CardGame> result = gameService.page(page, queryWrapper);
        return  new ApiResult(1,"成功",new PageBean<CardGame>(result) );
    }

    @GetMapping("/info/{gameid}")
    @ApiOperation(value = "活动信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="gameid",value = "活动id",example = "1",required = true)
    })
    public ApiResult<CardGame> info(@PathVariable int gameid) {
        //TODO：任务4.2-活动模块-活的信息
        CardGame game = gameService.getById(gameid);
        return new ApiResult<CardGame>(1, "成功", game);
    }

    @GetMapping("/products/{gameid}")
    @ApiOperation(value = "奖品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="gameid",value = "活动id",example = "1",required = true)
    })
    public ApiResult<List<CardProductDto>> products(@PathVariable int gameid) {
        //TODO：任务4.3-活动模块-奖品信息
//        //新建空列表
//        List<CardProductDto> returnList= new ArrayList<>();
//        //构建查询
//        QueryWrapper<CardGameProduct> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("gameid", gameid);
//        List<CardGameProduct> products = gameProductService.list(queryWrapper);
//        //遍历card_game_product
//        for (CardGameProduct product : products) {
//
//            CardProductDto cardProduct = new CardProductDto();
//            //从productService中获取数据拷贝到DTO
//            BeanUtils.copyProperties(productService.getById(product.getProductid()), cardProduct);
//            //添加amount字段值
//            cardProduct.setAmount(product.getAmount());
//            returnList.add(cardProduct);
//        }
//        return new ApiResult<List<CardProductDto>>(1, "成功", returnList);

        return new ApiResult<List<CardProductDto>>(1, "成功", loadService.getByGameId(gameid));
    }

    @GetMapping("/hit/{gameid}/{curpage}/{limit}")
    @ApiOperation(value = "中奖列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="gameid",value = "活动id",dataType = "int",example = "1",required = true),
            @ApiImplicitParam(name = "curpage",value = "第几页",defaultValue = "1",dataType = "int", example = "1",required = true),
            @ApiImplicitParam(name = "limit",value = "每页条数",defaultValue = "10",dataType = "int",example = "3",required = true)
    })
    public ApiResult<PageBean<ViewCardUserHit>> hit(@PathVariable int gameid,@PathVariable int curpage,@PathVariable int limit) {
        //TODO：任务4.4-活动模块-中奖列表
        //1.构建查询条件
        QueryWrapper<ViewCardUserHit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("gameid", gameid);
        //2.构建分页条件
        Page<ViewCardUserHit> page = new Page<>(curpage, limit);
        //3.查询数据
        Page<ViewCardUserHit> result = viewCardUserHitService.page(page, queryWrapper);
        //4.封装数据到PageBean
        PageBean<ViewCardUserHit> pageBean = new PageBean<>(result);
        //5.返回数据
        return new ApiResult<PageBean<ViewCardUserHit>>(1, "成功", pageBean);
    }


}